package com.store.app.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.adapter.GuideShoppingHistoryAdapter;
import com.store.app.adapter.GuideShoppingNewAdapter;
import com.store.app.bean.EventBusBean;
import com.store.app.bean.GuideShoppingNewBean;
import com.store.app.bean.HistoryBean;
import com.store.app.http.Stores;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.ToastUtils;
import com.store.app.utils.Utils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * Created by Administrator on 2017/2/10.\
 * 特卖社区导购页面
 */
public class SpecialSell_ShoperActivity extends BaseActivity implements IOnHttpListener {
    private ImageView public_ll_return;
    private TextView tvTitle;
    private ListView lv_shopper;
    private ListView lv_history;
    private NetRequest netRequest;
    private GuideShoppingHistoryAdapter mAdapter;
    private List<HistoryBean> historyBeanList = new ArrayList<>();

    private List<GuideShoppingNewBean> list_shopping = new ArrayList<>();
    private GuideShoppingNewAdapter mAdapter_shopping;

    private Dialog dialog;
    private TextView tv_history;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speciasellshoper);
        initData();
        initView();
        setListeners();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initData() {
        netRequest = new NetRequest(this);
    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        lv_shopper = (ListView) findViewById(R.id.lv_shopper);
        lv_history = (ListView) findViewById(R.id.lv_history);
        tvTitle.setText("社区导购");
        TextView tv_right = (TextView) findViewById(R.id.public_tv_right);
        tv_right.setText("邀请");//邀请会员==>邀请
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mAdapter = new GuideShoppingHistoryAdapter(historyBeanList, this);
        lv_history.setAdapter(mAdapter);
        showProgressDialog("", "加载中...");
        mAdapter_shopping = new GuideShoppingNewAdapter(list_shopping, this, new GuideShoppingNewAdapter.OnClickOrder() {

            @Override
            public void argeeClick(final GuideShoppingNewBean bean) {
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(SpecialSell_ShoperActivity.this);
                dialogBuilder.create();
                LayoutInflater inflater = LayoutInflater.from(SpecialSell_ShoperActivity.this);
                View view = inflater.inflate(R.layout.if_argee_dialog, null);
                TextView submit = (TextView) view.findViewById(R.id.submit);
                TextView cancel = (TextView) view.findViewById(R.id.cancel);
                submit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        netRequest.requestSaleAssistantApprove(7, bean.getConsumer_id(), bean.getMobile(), MainActivity.member_id, Stores.stores_name);
                    }
                });
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });
                dialogBuilder.setView(view);
                dialog = dialogBuilder.show();
            }

            @Override
            public void refuseClick(final GuideShoppingNewBean bean) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SpecialSell_ShoperActivity.this);
                builder.setMessage("确定拒绝?")
                        .setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        netRequest.requestSaleAssistantReject(8, bean.getConsumer_id(), bean.getMobile(), MainActivity.member_id, Stores.stores_name);
                    }
                });
                builder.show();
            }

            @Override
            public void deleteClick(final GuideShoppingNewBean bean) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SpecialSell_ShoperActivity.this);
                builder.setMessage("确定解除导购?")//删除==>解除
                        .setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        netRequest.requestDeleteGuide(9, bean.getConsumer_id(), bean.getMobile(), MainActivity.member_id, Stores.stores_name);
                    }
                });
                builder.show();
            }
        });
        lv_shopper.setAdapter(mAdapter_shopping);


        //获取导购申请
        netRequest.requestSaleAssistantAppliedForStoresListPageFind(2);
        tv_history = (TextView) findViewById(R.id.tv_history);
    }

    private void setListeners() {
        findViewById(R.id.public_ll_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SpecialSell_ShoperActivity.this, InvitationMemberActivity.class);
                startActivity(intent);
            }
        });


    }

    @Override
    public void onAfinalFail() {
        dismissProgressDialog();
        ToastUtils.showTextToast(this, "请检查本地网络");

    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        dismissProgressDialog();
        if (requestId == 1) {
            try {
                tv_history.setVisibility(View.VISIBLE);
                JSONObject data = new JSONObject(result);
                Log.v("zyl", "data1:" + data.toString());
                Gson gson = new Gson();
                JSONArray listArr = data.getJSONArray("list");
                List<HistoryBean> OrderBeanList = new ArrayList<>();
                for (int i = 0; i < listArr.length(); i++) {
                    HistoryBean orderBean = gson.fromJson(listArr.getJSONObject(i).toString(), HistoryBean.class);
                    OrderBeanList.add(orderBean);
                }
                historyBeanList.clear();
                historyBeanList.addAll(OrderBeanList);
                mAdapter.notifyDataSetChanged();
                Utils.Setlistview(lv_history);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestId == 2) {
            try {
                JSONObject data = new JSONObject(result);
                Log.v("zyl", "data1:" + data.toString());
                Gson gson = new Gson();
                JSONArray listArr = data.getJSONArray("list");
                list_shopping.clear();

                for (int i = 0; i < listArr.length(); i++) {
                    GuideShoppingNewBean orderBean = gson.fromJson(listArr.getJSONObject(i).toString(), GuideShoppingNewBean.class);
                    list_shopping.add(orderBean);
                }
                mAdapter_shopping.notifyDataSetChanged();
                Utils.Setlistview(lv_shopper);


                //获取历史记录
                netRequest.requestSaleAssistantHistoryPageFind(1, "0", "0");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestId == 7) {
            Log.v("zyl", "批准成功");
            netRequest.requestSaleAssistantAppliedForStoresListPageFind(2);
            netRequest.requestSaleAssistantHistoryPageFind(1, "0", "0");
        } else if (requestId == 8) {
            Log.v("zyl", "拒绝成功");
            netRequest.requestSaleAssistantAppliedForStoresListPageFind(2);
            netRequest.requestSaleAssistantHistoryPageFind(1, "0", "0");
        } else if (requestId == 9) {
            Log.v("zyl", "删除成功");
            netRequest.requestSaleAssistantAppliedForStoresListPageFind(2);
            netRequest.requestSaleAssistantHistoryPageFind(1, "0", "0");
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        dismissProgressDialog();
        ToastUtils.showTextToast(this, msg);
    }

    //邀请会员
    private void onEvent(EventBusBean eventBusBean) {
        if (eventBusBean.type == 226) {
            netRequest.requestSaleAssistantAppliedForStoresListPageFind(2);
            netRequest.requestSaleAssistantHistoryPageFind(1, "0", "0");
        }
    }
}
